package coom;


import android.util.Log;

import net.ck.hzq.TobuyApplication;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import util.StringUtils;

/**
 * Created by chenjian on
 */
public class RetrofitHelp {

    // 测试服
//    public final static String BASE_URL = "http://116.62.222.151:8080/";
    // 正式服
    public final static String BASE_URL = "http://47.111.188.60:8080/";

    public static final int DEFAULT_TIMEOUT = 120;

    private Retrofit retrofit;

    private static ApiService openApi;

    private static ApiService secretApi;

    /**
     * 正常访问
     */
    private RetrofitHelp() {
        /*
         **打印retrofit信息部分
         */
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.e("RetrofitLog","retrofitBack = "+message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        httpClientBuilder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        httpClientBuilder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        httpClientBuilder.retryOnConnectionFailure(true);
        httpClientBuilder.addInterceptor(loggingInterceptor);
        httpClientBuilder.authenticator(new TokenAuthenticator());
        httpClientBuilder.addNetworkInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
//                        .addHeader("Content-Type", "text/html;application/xhtml+xml;application/xml;charset=utf-8")
//                        .addHeader("Accept-Encoding", "gzip,deflate")
//                        .addHeader("Accept-Language", "zh-CN,zh")
//                        .addHeader("Connection", "keep-alive")
//                        .addHeader("Accept", "text/html,application/xhtml+xml,application/xml")
                        .addHeader("Accept-Encoding", "gzip,deflate")
                        .addHeader("Connection", "keep-alive")
                        .addHeader("Accept", "*/*")
//                        .addHeader("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NTksImV4cCI6MTU1NTQwMjYzMn0._EkxBWnNITymO32nL4wodbZMY8ROO_BiZ1DQXcTVwuU")
                        .addHeader("Authorization", StringUtils.isEmpty(TobuyApplication.token) ? "" : TobuyApplication.token)
                        .build();
                return chain.proceed(request);
            }
        });

        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
    }


    //不需要登录
    public static ApiService getOpenApi() {
        if (openApi == null) {
            openApi = new RetrofitHelp().retrofit.create(ApiService.class);
        }
        return openApi;
    }

    //登录后
    public static ApiService getSecretApi() {
        if (secretApi == null) {
            secretApi = new RetrofitHelp().retrofit.create(ApiService.class);
        }
        return secretApi;
    }


    public static void logout() {
        secretApi = null;
    }
}
